package pab.ui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import javax.swing.UIManager;

import pab.core.exception.AccountException;
import pab.core.exception.TransactionException;
import pab.core.service.AccountService;
import pab.core.service.AccountServiceImpl;
import pab.core.service.ExchangeRate;
import pab.core.service.FixedExchangeRate;
import pab.core.service.WebExchangeRate;


public class MainApp {

    public static final String APP_HOME = System.getProperty("user.dir");
    public static void main(String[] args) throws Exception {

        try{
            UIManager.setLookAndFeel("com.easynth.lookandfeel.EaSynthLookAndFeel");
            String propPath = APP_HOME + "/data/user.properties";
            File isExist = new File(propPath);
            if (!isExist.exists()){
                FileOutputStream fos = new FileOutputStream(propPath);
                fos.close();
            }

            Properties prop = new Properties();

            FileInputStream fis = new FileInputStream(propPath);
            prop.load(fis);
            fis.close();
            InitialPassword init = null;
            if (prop.getProperty("user")==null){//chua khoi tao user -> man hinh tao moi user
                init = new InitialPassword(null, propPath);
                init.setVisible(true);
                if(!init.isOk)
                    System.exit(0);

            }	

            if(init == null){// khoi tao user roi, direct vao login
                Login login = new Login(propPath);
                login.setVisible(true);
                if (login.isLoginOK){
                    initService();
                }else{
                    System.exit(0);
                }
            }else if (init.isOk == true){// vua moi khoi tao user xong
                Login login = new Login(propPath);
                login.setVisible(true);
                if (login.isLoginOK){
                    initService();
                }else
                {
                    System.exit(0);
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(APP_HOME);

    }
    
    volatile static ExchangeRate exchangeRate = null;
    private static void initService() throws AccountException {
        
        /*Thread thread = new Thread(new Runnable() {
            
            public void run() {
                try {
                exchangeRate = new WebExchangeRate();
            } catch (TransactionException e) {
                exchangeRate = new FixedExchangeRate();
            }
                
            }
        });
        
        thread.run();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new AccountException(e);
        }
        
        if (exchangeRate == null) {
            thread.interrupt();
            exchangeRate = new FixedExchangeRate();
        }*/
        try {
            exchangeRate = new WebExchangeRate();
        } catch (TransactionException e) {
            exchangeRate = new FixedExchangeRate();
        }
        final AccountService accountService = new AccountServiceImpl(APP_HOME + "/data/accounts.xml", exchangeRate);
        MainForm l = new MainForm(accountService);
        l.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                accountService.save();
            }
        });
        l.setVisible(true);
    }
}
